package com.beiding.my.bemapping;

import com.beiding.my.dtmapping.DtTypeMapper;

import java.util.HashMap;
import java.util.Map;


/*
    be类型映射器

 */
public class BeTypeMapper<T> {

    BeTypeMapper() {
    }

    //类型id
    private String id = Id.bot();

    //be名称和be实体简称一致
    String name;

    //be实际类型,可用于创建实体等
    Class<T> type;

    //数据实体映射
    DtTypeMapper dtTypeMapper;

    //分别为 名称-属性 和 id-属性
    Map<String, BeAttributeMapper> nameBeAttributeMapperMap = new HashMap<>();
    Map<String, BeAttributeMapper> idBoAttributeMapperMap = new HashMap<>();

    void addAttributeMapper(BeAttributeMapper mapper) {
        nameBeAttributeMapperMap.put(mapper.getName(), mapper);
        idBoAttributeMapperMap.put(mapper.getId(), mapper);
    }


    //使用属性名称构建be实例
    public T newBeByAttributeName(Map<String, Object> values) {
        T be = newBe();
        values.forEach((k, v) -> {
            String[] segments = k.split("\\.");
            assignmentByAttributeName(be, segments, 0, v);
        });
        return be;

    }

    //使用属性id构建实例
    public T newBeByAttributeId(Map<String, Object> values) {
        T bo = newBe();
        values.forEach((k, v) -> {
            String[] segments = k.split("_");
            assignmentByAttributeId(bo, segments, 0, v);
        });
        return bo;
    }

    //使用属性Id区段进行赋值操作
    public void assignmentByAttributeId(Object be, String[] segments, int currentSegment, Object v) {
        BeAttributeMapper boAttributeMapper = idBoAttributeMapperMap.get(segments[currentSegment]);
        if (boAttributeMapper == null) {
            throw new RuntimeException("不存在的Bo成员Id:" + type.getName() + ":" + segments[currentSegment]);
        } else {
            boAttributeMapper.assignmentByAttributeId(be, segments, currentSegment, v);
        }
    }

    ///使用属性名区段赋值操作
    public void assignmentByAttributeName(Object be, String[] segments, int currentSegment, Object v) {
        BeAttributeMapper boAttributeMapper = nameBeAttributeMapperMap.get(segments[currentSegment]);
        if (boAttributeMapper == null) {
            throw new RuntimeException("不存在的Bo成员名称:" + type.getName() + ":" + segments[currentSegment]);
        } else {
            boAttributeMapper.assignmentByAttributeName(be, segments, currentSegment, v);
        }
    }

    //创建be实体
    public T newBe() {
        try {
            return type.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //填充be条件或返回表达式的详情
    void fillExpressionDetails(String[] segments, int currentSegment, ExpressionDetails expressionDetails) {

        //填充所有字段类型的数据
        if (segments[currentSegment].equals("*")) {

            for (BeAttributeMapper boAttributeMapper : nameBeAttributeMapperMap.values()) {
                if (boAttributeMapper instanceof BeFieldMapper) {
                    boAttributeMapper.fillExpressionDetails(dtTypeMapper.getType(), segments, currentSegment, expressionDetails);
                }
            }

        } else {
            BeAttributeMapper boAttributeMapper = nameBeAttributeMapperMap.get(segments[currentSegment]);
            if (boAttributeMapper == null) {
                throw new RuntimeException("不存在的Bo成员名称:" + type.getName() + ":" + segments[currentSegment]);
            } else {
                boAttributeMapper.fillExpressionDetails(dtTypeMapper.getType(), segments, currentSegment, expressionDetails);
            }
        }
    }

    //获取表达式转换详情
    public ExpressionDetails getExpressionDetails(String name, String expression) {
        ExpressionDetails expressionDetails = new ExpressionDetails(name);
        String[] split = expression.split("\\.");
        fillExpressionDetails(split, 0, expressionDetails);
        return expressionDetails;
    }

    public String getBeTable() {
        return dtTypeMapper.getTableName();
    }


}
